import { findAllTower, findTowerById } from '@/apis/towerAPI'
import { PageModel, towerModel } from '@/types/model'
import { pageModel } from '@/util/pageModel'
import { scXY } from '@/util/scaleXY'
import { ref } from 'vue'

export default () => {
  const towers = ref<PageModel<towerModel>>()
  const tower = ref<towerModel>()

  // 获取所有的塔
  const all = async (args = {}) => {
    const { data } = await findAllTower(args)
    towers.value = pageModel<towerModel>(data, args)
  }

  // 获取所有铁塔的坐标
  const points = async (startY: number) => {
    towers.value?.data.forEach((el) => {
      const [x, y] = JSON.parse(el.xy!)
      el.points = scXY(x, y, startY)
    })
  }

  async function find(id: number) {
    const { data } = await findTowerById(id)
    tower.value = data
    return data
  }

  return { towers, find, points, all }
}
